import { BrowserWindow, ipcMain, Menu, Tray } from "electron";
import path from "path";
import { TimerDefaultType, TimerMode } from "../renderer/src/types/GlobalEnum";
import { CacheSetting } from "../renderer/src/types/types";
import FontSettingWindow from "./wins/fontSetting";

const createTray = (mainWindow: BrowserWindow) => {
    const tray = new Tray(
        path.resolve(
            __dirname,
            process.platform == "darwin"
                ? "../../resources/IconTemplate@2x.png"
                : "../../resources/IconTemplate@2x.png"
        )
    );
    tray.setToolTip("cclock");
    ipcMain.on("sendCacheSettings", (_, settings: CacheSetting) => {
        // 设置托盘菜单栏
        const contextMenu = Menu.buildFromTemplate([
            {
                label: "计时模式",
                submenu: [
                    {
                        label: "正计时",
                        type: "radio",
                        checked: settings.mode === TimerMode.positive,
                        click: () =>
                            mainWindow.webContents.send(
                                "changeMode",
                                TimerMode.positive
                            ),
                    },
                    {
                        label: "倒计时",
                        type: "radio",
                        checked: settings.mode === TimerMode.reverse,
                        click: () =>
                            mainWindow.webContents.send(
                                "changeMode",
                                TimerMode.reverse
                            ),
                    },
                ],
            },
            {
                label: "计时时间",
                submenu: [
                    {
                        label: "60min",
                        type: "radio",
                        checked:
                            settings.defaultTime ===
                            TimerDefaultType.SIXTY_MINS,
                        click: () =>
                            mainWindow.webContents.send(
                                "changeDefaultTime",
                                TimerDefaultType.SIXTY_MINS
                            ),
                    },
                    {
                        label: "30min",
                        type: "radio",
                        checked:
                            settings.defaultTime ===
                            TimerDefaultType.THIRTY_MINS,
                        click: () =>
                            mainWindow.webContents.send(
                                "changeDefaultTime",
                                TimerDefaultType.THIRTY_MINS
                            ),
                    },
                    {
                        label: "15min",
                        type: "radio",
                        checked:
                            settings.defaultTime ===
                            TimerDefaultType.FIFTEEN_MINS,
                        click: () =>
                            mainWindow.webContents.send(
                                "changeDefaultTime",
                                TimerDefaultType.FIFTEEN_MINS
                            ),
                    },
                    {
                        label: "10min",
                        type: "radio",
                        checked:
                            settings.defaultTime === TimerDefaultType.TEN_MINS,
                        click: () =>
                            mainWindow.webContents.send(
                                "changeDefaultTime",
                                TimerDefaultType.TEN_MINS
                            ),
                    },
                    {
                        label: "5min",
                        type: "radio",
                        checked:
                            settings.defaultTime === TimerDefaultType.FINE_MINS,
                        click: () =>
                            mainWindow.webContents.send(
                                "changeDefaultTime",
                                TimerDefaultType.FINE_MINS
                            ),
                    },
                    {
                        label: "1min",
                        type: "radio",
                        checked:
                            settings.defaultTime === TimerDefaultType.ONE_MINS,

                        click: () =>
                            mainWindow.webContents.send(
                                "changeDefaultTime",
                                TimerDefaultType.ONE_MINS
                            ),
                    },
                    // {
                    //     label: "自定义",
                    //     type: "radio",
                    //     checked:
                    //         settings.defaultTime === TimerDefaultType.CUSTOM,
                    //     click: () =>
                    //         mainWindow.webContents.send(
                    //             "changeDefaultTime",
                    //             TimerDefaultType.CUSTOM
                    //         ),
                    // },
                ],
            },
            { type: "separator" },
            {
                label: "12小时制",
                type: "checkbox",
                checked: !settings.isTwentyFour,
                click: () => mainWindow.webContents.send("changeDateSystem"),
            },
            {
                label: "显示秒",
                type: "checkbox",
                checked: !settings.isShowSeconds,
                click: () => mainWindow.webContents.send("changeShowSeconds"),
            },
            { type: "separator" },
            {
                label: "字体",
                click: () => new FontSettingWindow().windowInstance.show(),
            },
            // {
            //     label: "见万事如流",
            //     click: () => shell.openExternal("http://www.chenyx.site"),
            // },
            { type: "separator" },
            { label: "退出", role: "quit" },
        ]);
        tray.setContextMenu(contextMenu);
    });
};

export default createTray;
